//
//  AXFileManagerExtension.swift
//  AutoXcodeProj
//
//  Created by jenkins on 2021/7/1.
//

import Foundation

public extension FileManager {
    // MARK: 1.1、获取Home的完整路径名
    /// 获取Home的完整路径名
    /// - Returns: Home的完整路径名
    static func gethomeDirectory() -> String {
        //获取程序的Home目录
        let homeDirectory = NSHomeDirectory()
        return homeDirectory
    }
    
    // 判断是否是文件 or 文件夹
    static func directoryIsExists (path: String) -> Bool {
        var directoryExists = ObjCBool.init(false)
        let fileExists = FileManager.default.fileExists(atPath: path, isDirectory: &directoryExists)
        return fileExists && directoryExists.boolValue
    }
    
    // 判断文件是否存在
    static func fileIsExists (path: String) -> Bool {
        let fileExists = FileManager.default.fileExists(atPath: path)
        return fileExists
    }
    
    //获取指定后缀文件名
    static func getXcodeprojFilePath(_ dirPath: String) -> String? {
        do {
            let docs = try FileManager.default.contentsOfDirectory(atPath: dirPath)
            let images = docs.filter{$0.contains(".xcodeproj")}
            return images.last
        } catch {
            print(error)
            return nil
        }
    }
    

   
    
    
    static func getAllFilePath(_ dirPath: String) -> [String]? {
        var filePaths = [String]()
        do {
            let array = try FileManager.default.contentsOfDirectory(atPath: dirPath)
            for fileName in array {
                var isDir: ObjCBool = true
                let fullPath = "\(dirPath)/\(fileName)"
                if FileManager.default.fileExists(atPath: fullPath, isDirectory: &isDir) {
                    if !isDir.boolValue {
                        filePaths.append(fullPath)
                    }
                }
            }
        } catch let error as NSError {
            print("get file path error: \(error)")
        }
        return filePaths;
    }
}
